package com.nageoffer.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nageoffer.shortlink.common.convention.result.Result;
import com.nageoffer.shortlink.common.convention.result.Results;
import com.nageoffer.shortlink.common.dto.project.req.ShortLinkBatchCreateReqDTO;
import com.nageoffer.shortlink.common.dto.project.req.ShortLinkCreateReqDTO;
import com.nageoffer.shortlink.common.dto.project.req.ShortLinkUpdateReqDTO;
import com.nageoffer.shortlink.common.dto.project.resp.ShortLinkBatchCreateRespDTO;
import com.nageoffer.shortlink.common.dto.project.resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.common.dto.project.resp.ShortLinkGroupCountRespDTO;
import com.nageoffer.shortlink.common.dto.project.resp.ShortLinkPageRespDTO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.nageoffer.shortlink.project.handler.CustomBlockHandler;
import com.nageoffer.shortlink.project.service.ShortLinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortLinkService;

    /**
     *页查询短链接重定向
     * @param shortUri 短链接
     * @return
     */
    @GetMapping("/{short-uri}")
    public void redirect(
            @PathVariable("short-uri") String shortUri,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        shortLinkService.shortLinkRedirect(shortUri,request,response);
    }

    /**
     * 创建短链接
     * @param reqParam
     *
     * @return
     * 组id
     * 原链接
     * 完整短链接
     */
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    @PostMapping("/api/short-link/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO reqParam) throws IOException {
        ShortLinkCreateRespDTO resp = shortLinkService.createShortLink(reqParam);
        return Results.success(resp);
    }
    /**
     * 修改短链接
     * @param reqParam
     *
     * @return 无
     */
    @PutMapping("/api/short-link/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO reqParam){
        shortLinkService.updateShortLink(reqParam);
        return Results.success();
    }

    /**
     * 批量创建
     * @param reqParam
     * @return
     */
    @PostMapping("/api/short-link/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> createBatchShortLink(@RequestBody ShortLinkBatchCreateReqDTO reqParam) throws IOException {
        ShortLinkBatchCreateRespDTO resp = shortLinkService.createBatchShortLink(reqParam);
        return Results.success(resp);
    }

    /**
     * 分页查询分组下的短链接
     * @param reqParam
     * 组标识: gid
     * 起始页号: current
     * 页面大小: size
     * @return 分页短链接
     */
    @GetMapping("/api/short-link/page")
    public Result<Page<ShortLinkPageRespDTO>> getShortLinkPage(ShortLinkPageReqDTO reqParam){
        Page<ShortLinkPageRespDTO> resp = shortLinkService.getShortLinkPage(reqParam);
        return Results.success(resp);
    }

    /**
     *
     * @param reqParam (gid) list
     * @return (gid 短链接数) list
     */
    @GetMapping("/api/short-link/count")
    public Result<List<ShortLinkGroupCountRespDTO>> groupShortLinkCount(@RequestParam("reqParam") List<String> reqParam){
        List<ShortLinkGroupCountRespDTO> resp = shortLinkService.groupShortLinkCount(reqParam);
        return Results.success(resp);
    }

}
